﻿using System;
using System.Linq;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.ServiceLocation;
using Infrastucture.Navigation;

namespace Infrastucture.Controllers
{
    public class Controller
    {
        protected readonly IRegionManager RegionManager;

        public Controller(IRegionManager regionManager)
        {
            this.RegionManager = regionManager;
        }
        public Controller()
        {
            this.RegionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
        }

        // TEST Navigation Change
        public void Navigate(NavigationLink link)
        {
            var r = this.RegionManager.Regions[link.RegionName];
            var views = r.Views;
            var v = r.Views.FirstOrDefault() as IGlobalNavigate;
            v.NavigateByLink(link);
            // would need view to inherit the method
        }
    }

    public class Controller<View> : IController<View>
    {
        protected readonly IRegionManager RegionManager;

        public Controller(IRegionManager regionManager)
        {
            this.RegionManager = regionManager;
        }
        public Controller()
        {
            this.RegionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
        }

        public void NavigateToView()
        {
            Uri uri = new Uri(typeof(View).Name, UriKind.Relative);
            RegionManager.RequestNavigate(Infrastucture.RegionNames.MainRegion, uri);
        }

        public void NavigateToView(string regionName)
        {
            Uri uri = new Uri(typeof(View).Name, UriKind.Relative);
            RegionManager.RequestNavigate(regionName, uri);
        }


        public void NavigateToItemView(Guid id, bool isParentid)
        {
            if (isParentid)
            {
                Uri uri = new Uri(typeof(View).Name
                    + "?parentid=" + id.ToString(), UriKind.Relative);
                RegionManager.RequestNavigate(RegionNames.MainRegion, uri);
            }
            else
            {
                Uri uri = new Uri(typeof(View).Name
                    + "?id=" + id.ToString(), UriKind.Relative);
                RegionManager.RequestNavigate(RegionNames.MainRegion, uri);
            }
        }


        public void Navigate(NavigationLink link)
        {
            var r = this.RegionManager.Regions[link.RegionName];
            var views = r.Views;
            var v = r.Views.FirstOrDefault() as IGlobalNavigate;

            if (v != null)
                v.NavigateByLink(link);
        }
    }

    public class Controller<ListView, ItemView> : IController<ListView, ItemView>
    {
        protected readonly IRegionManager RegionManager;

        public Controller(IRegionManager regionManager)
        {
            this.RegionManager = regionManager;
        }
        public Controller()
        {
            this.RegionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
        }

        public void NavigateToListView()
        {
            Uri uri = new Uri(typeof(ListView).Name, UriKind.Relative);
            RegionManager.RequestNavigate(Infrastucture.RegionNames.MainRegion, uri);
        }

        public void NavigateToItemView()
        {
            Uri uri = new Uri(typeof(ItemView).Name, UriKind.Relative);
            RegionManager.RequestNavigate(Infrastucture.RegionNames.MainRegion, uri);
        }

        public void NavigateToItemView(Guid id)
        {
            Uri uri = new Uri(typeof(ItemView).Name
                + "?id=" + id.ToString(), UriKind.Relative);
            RegionManager.RequestNavigate(RegionNames.MainRegion, uri);
        }

        public void NavigateToItemView(Guid id, bool isParentid)
        {
            if (isParentid)
            {
                Uri uri = new Uri(typeof(ItemView).Name
                    + "?parentid=" + id.ToString(), UriKind.Relative);
                RegionManager.RequestNavigate(RegionNames.MainRegion, uri);
            }
            else
            {
                Uri uri = new Uri(typeof(ItemView).Name
                    + "?id=" + id.ToString(), UriKind.Relative);
                RegionManager.RequestNavigate(RegionNames.MainRegion, uri);
            }
        }


        public void Navigate(NavigationLink link)
        {
            var r = this.RegionManager.Regions[link.RegionName];
            var views = r.Views;
            var v = r.Views.FirstOrDefault() as IGlobalNavigate;

            if (v != null)
                v.NavigateByLink(link);
        }
    }
}
